/*******************************************************************************
 * Copyright 2012 Chao Zhang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.jbiolib.ncbi.genbank;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.jbiolib.io.FileRead;
import org.jbiolib.io.FileWrite;
import org.jbiolib.ncbi.NCBIReference;

/**
 * @author Chao Zhang
 * @version 1.0 2012-11-06
 */

public class NCBIGenBankPaser {

    public NCBIGenBankPaser() {
    }

    private boolean checkList(String locus, String[] genomeList) {
        for(String genomeLocus:genomeList) {
            if((genomeLocus.trim().indexOf(locus)!=-1)||(locus.indexOf(genomeLocus.trim())!=-1))
                return true;
        }
        return false;
    }

    public void splitWholeGenomesForEcoli(String inputFilePath, String inputfile, String genename){
        String[] wholeGenomeList = FileRead.readFile(inputFilePath+genename, new String[0]);
        ArrayList tempDataArray = new ArrayList();
        NCBIGenBank loadingResult = new NCBIGenBank();
        try {
            FileWriter writerWhole = new FileWriter(inputFilePath+inputfile.substring(0,inputfile.indexOf(".gb"))+".host.gb", true);
			BufferedWriter bufWriterWhole = new BufferedWriter(writerWhole);
            //FileWriter writerPart = new FileWriter(inputFilePath+inputfile+".yes.gb", true);
			//BufferedWriter bufWriterPart = new BufferedWriter(writerPart);
			
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath+inputfile));
            String line = "";
            String locus = "";
            boolean locusTag = false;
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        locus = line.substring(12, 28).trim();
                        locusTag = checkList(locus, wholeGenomeList);
                        if(locusTag) {
                            bufWriterWhole.write(line);
                            bufWriterWhole.newLine();
                        } else {
                            //bufWriterPart.write(line);
                            //bufWriterPart.newLine();
                        }
                    } else {
                        if(locusTag) {
                            bufWriterWhole.write(line);
                            bufWriterWhole.newLine();
                        } else {
                            //bufWriterPart.write(line);
                            //bufWriterPart.newLine();
                        }
                    }
                }
            } while(line != null);
            in.close();
            bufWriterWhole.close();
			writerWhole.close();
            //bufWriterPart.close();
			//writerPart.close();
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            e.printStackTrace();
        }
    }

    public void checkHostOnMultiGenBankFileForEcoli(String inputFilePath, String filename){
        ArrayList tempDataArray = new ArrayList();
        NCBIGenBank loadingResult = new NCBIGenBank();
        try {
            int index = 0;
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath+filename));
            String line = "";
            String locus = "";
            String host = "";
            String gene = "";
            String organ = "";
            String gi = "";
            int i = 0;
            Set geneSet = new HashSet();
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        i++;
                        host = "";
                        organ = "";
                        gi = "";
                        locus = line.substring(12, 28).trim();
                        geneSet = new HashSet();
                        index++;
                        tempDataArray = new ArrayList();
                        tempDataArray.add(line);
                    } else if(tag.equals("VERSION")) {
                        gi = line.substring(line.indexOf("GI:")+3, line.length()).trim();
                    } else if (tag.equals("//")) {
                        System.out.println(locus);
                        if(!host.equals(""))
                            FileWrite.writeInput(locus+"\t"+gi+"\t"+host+"\t"+organ+"\t"+geneSet.toString().substring(1, geneSet.toString().length()-1), inputFilePath+filename+".result");
                    } else {
                        if(line.indexOf("/host")!=-1) {
                            String tempLine = line;
                            while(!tempLine.substring(tempLine.length()-1, tempLine.length()).equals("\"")) {
                                line = in.readLine();
                                tempLine+=line;
                            }
                            host = tempLine.substring(tempLine.indexOf("=")+2, tempLine.lastIndexOf("\"")).trim();
                        } else if(line.indexOf("/gene=")!=-1) {
                            String tempLine = line;
                            while(!tempLine.substring(tempLine.length()-1, tempLine.length()).equals("\"")) {
                                line = in.readLine();
                                tempLine+=line;
                            }
                            geneSet.add(tempLine.substring(tempLine.indexOf("=")+2, tempLine.lastIndexOf("\"")).trim());
                        } else if(line.indexOf("/organism")!=-1) {
                            String tempLine = line;
                            while(!tempLine.substring(tempLine.length()-1, tempLine.length()).equals("\"")) {
                                line = in.readLine();
                                tempLine+=line;
                            }
                            organ = tempLine.substring(tempLine.indexOf("=")+2, tempLine.lastIndexOf("\"")).trim();
                        }
                    }
                }
            } while(line != null);
            in.close();
            System.out.println(i);
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            e.printStackTrace();
        }
    }
    
    public void loadMultiGenBankFileForEcoli(String inputFilePath, String geneName){
        ArrayList tempDataArray = new ArrayList();
        NCBIGenBank loadingResult = new NCBIGenBank();
        try {
            int index = 0;
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath));
            String line = "";
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        index++;
                        tempDataArray = new ArrayList();
                        tempDataArray.add(line);
                    } else if (tag.equals("//")) {
                        tempDataArray.add(line);
                        Object[] arr = tempDataArray.toArray();
                        String[] dataArr = new String[arr.length];
                        for(int i=0;i<arr.length;i++) {
                            dataArr[i]=arr[i].toString();
                        }
                        loadingResult = this.loadGenBankFile(dataArr);
                        writeHostInfoToFile(loadingResult, inputFilePath+".result", geneName);
                    } else {
                        tempDataArray.add(line);
                    }
                }
            } while(line != null);
            in.close();
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            e.printStackTrace();
        }
    }

    public void generateAllProtein(String inputFilePath){
        ArrayList tempDataArray = new ArrayList();
        NCBIGenBank loadingResult = new NCBIGenBank();
        try {
            int index = 0;
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath));
            String line = "";
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        index++;
                        tempDataArray = new ArrayList();
                        tempDataArray.add(line);
                    } else if (tag.equals("//")) {
                        tempDataArray.add(line);
                        Object[] arr = tempDataArray.toArray();
                        String[] dataArr = new String[arr.length];
                        for(int i=0;i<arr.length;i++) {
                            dataArr[i]=arr[i].toString();
                        }
                        loadingResult = this.loadGenBankFile(dataArr);
                        try {
                        //if(!loadingResult.getFeaturesBasicInfo().getHost().equals("")) {
                            for(NCBIGenBankFeaturesCDS cdsInfo:loadingResult.getFeaturesCDSInfo()) {
                                FileWrite.writeInput(">"+loadingResult.getLocus()+"#"+replacePrime(loadingResult.getFeaturesBasicInfo().getHost())+"#"+replacePrime(cdsInfo.getCDS_protein_id())+"#"+replacePrime(cdsInfo.getGene_gene()), inputFilePath.substring(0,inputFilePath.indexOf(".gb"))+".pro.fas");
                                FileWrite.writeInput(replacePrime(cdsInfo.getCDS_translation().replace(" ", "")), inputFilePath.substring(0,inputFilePath.indexOf(".gb"))+".pro.fas");
                            }
                        //}
                    } catch (Exception e) {
                        System.out.println(loadingResult.getLocus()+" has some problem");
                        e.printStackTrace();
                    }
                    } else {
                        tempDataArray.add(line);
                    }
                }
            } while(line != null);
            in.close();
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            e.printStackTrace();
        }
    }

    public void generateAllProtein2Fasta(String inputFilePath){
        ArrayList tempDataArray = new ArrayList();
        NCBIGenBank loadingResult = new NCBIGenBank();
        try {
            int index = 0;
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath));
            String line = "";
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        index++;
                        tempDataArray = new ArrayList();
                        tempDataArray.add(line);
                    } else if (tag.equals("//")) {
                        tempDataArray.add(line);
                        Object[] arr = tempDataArray.toArray();
                        String[] dataArr = new String[arr.length];
                        for(int i=0;i<arr.length;i++) {
                            dataArr[i]=arr[i].toString();
                        }
                        loadingResult = this.loadGenBankFile(dataArr);
                        try {
                        //if(!loadingResult.getFeaturesBasicInfo().getHost().equals("")) {
                            for(NCBIGenBankFeaturesCDS cdsInfo:loadingResult.getFeaturesCDSInfo()) {
                                FileWrite.writeInput(">"+loadingResult.getLocus()+"_"+replacePrime(cdsInfo.getGene_gene()), inputFilePath+".pro.fas");
                                FileWrite.writeInput(replacePrime(cdsInfo.getCDS_translation().replace(" ", "")), inputFilePath+".pro.fas");
                            }
                        //}
                    } catch (Exception e) {
                        System.out.println(loadingResult.getLocus()+" has some problem");
                        e.printStackTrace();
                    }
                    } else {
                        tempDataArray.add(line);
                    }
                }
            } while(line != null);
            in.close();
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            e.printStackTrace();
        }
    }

    public boolean writeHostInfoToFile(NCBIGenBank loadingResult, String outputfile, String geneName) {
        //System.out.println(loadingResult.getFeaturesCDSInfo().size());
        boolean sucTag = true;
        try {
            //if(!loadingResult.getFeaturesBasicInfo().getHost().equals("")) {
                for(NCBIGenBankFeaturesCDS cdsInfo:loadingResult.getFeaturesCDSInfo()) {
                    if(cdsInfo.getGene_gene().toUpperCase().equals(geneName.toUpperCase())) {
                        String regionString = replacePrime(cdsInfo.getGene_region());
                        int startIndex = 0;
                        int endIndex = 0;
                        if(regionString.indexOf("(")!=-1){
                            if(regionString.indexOf("(<")!=-1)
                                startIndex = new Integer(regionString.substring(regionString.indexOf("(<")+2,regionString.indexOf("..")).trim()).intValue();
                            else
                                startIndex = new Integer(regionString.substring(regionString.indexOf("(")+1,regionString.indexOf("..")).trim()).intValue();
                            if(regionString.indexOf("..>")!=-1)
                                endIndex = new Integer(regionString.substring(regionString.indexOf("..>")+3,regionString.indexOf(")")).trim()).intValue();
                            else
                                endIndex = new Integer(regionString.substring(regionString.indexOf("..")+2,regionString.indexOf(")")).trim()).intValue();
                        } else if(regionString.indexOf("<")!=-1){
                            startIndex = new Integer(regionString.substring(regionString.indexOf("<")+1,regionString.indexOf("..")).trim()).intValue();
                            if(regionString.indexOf("..>")!=-1)
                                endIndex = new Integer(regionString.substring(regionString.indexOf("..>")+3,regionString.length()).trim()).intValue();
                            else
                                endIndex = new Integer(regionString.substring(regionString.indexOf("..")+2,regionString.length()).trim()).intValue();
                        } else {
                            startIndex = new Integer(regionString.substring(0,regionString.indexOf("..")).trim()).intValue();
                            if(regionString.indexOf("..>")!=-1)
                                endIndex = new Integer(regionString.substring(regionString.indexOf("..>")+3,regionString.length()).trim()).intValue();
                            else
                                endIndex = new Integer(regionString.substring(regionString.indexOf("..")+2,regionString.length()).trim()).intValue();
                        }
                        String writeString = loadingResult.getLocus() + "\t" +
                            replacePrime(loadingResult.getFeaturesBasicInfo().getHost())+ "\t" +
                            replacePrime(loadingResult.getFeaturesBasicInfo().getStrain())+ "\t"+
                            replacePrime(cdsInfo.getGene_gene())+ "\t"+
                            replacePrime(cdsInfo.getGene_region())+ "\t"+
                            replacePrime(loadingResult.getOrgin().replace(" ", "")).substring(startIndex-1, endIndex)+ "\t"+
                            replacePrime(cdsInfo.getCDS_translation().replace(" ", ""));

                        FileWrite.writeInput(writeString, outputfile);
                        FileWrite.writeInput(">"+loadingResult.getLocus()+"_"+replacePrime(loadingResult.getFeaturesBasicInfo().getHost()), outputfile+".gene.fas");
                        FileWrite.writeInput(replacePrime(loadingResult.getOrgin().replace(" ", "")).substring(startIndex-1, endIndex), outputfile+".gene.fas");
                        FileWrite.writeInput(">"+loadingResult.getLocus()+"_"+replacePrime(loadingResult.getFeaturesBasicInfo().getHost()), outputfile+".pro.fas");
                        FileWrite.writeInput(replacePrime(cdsInfo.getCDS_translation().replace(" ", "")), outputfile+".pro.fas");
                    }
                }
            //}
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            sucTag = false;
            e.printStackTrace();
        }
        return sucTag;
    }

    public void loadMultiGenBankFile(String inputFilePath){
        ArrayList tempDataArray = new ArrayList();
        NCBIGenBank loadingResult = new NCBIGenBank();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/hpbase2";
            Connection con = DriverManager.getConnection(url,"root", "");
            Statement stmt = con.createStatement();
            
            int index = 0;
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath));
            String line = "";
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        index++;
                        tempDataArray = new ArrayList();
                        tempDataArray.add(line);
                    } else if (tag.equals("//")) {
                        tempDataArray.add(line);
                        Object[] arr = tempDataArray.toArray();
                        String[] dataArr = new String[arr.length];
                        for(int i=0;i<arr.length;i++) {
                            dataArr[i]=arr[i].toString();
                        }
                        loadingResult = this.loadGenBankFile(dataArr);
                        writeToFile(loadingResult, stmt);
                        //writeGenBankFileIntoDB(loadingResult, stmt);
                    } else {
                        tempDataArray.add(line);
                    }
                }
            } while(line != null);
            con.close();
            in.close();
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            e.printStackTrace();
        }
    }

    public NCBIGenBank loadOneGenBankFile(String inputFilePath) {
        NCBIGenBank loadingResult = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/hpbase1";
            Connection con = DriverManager.getConnection(url,"root", "");
            Statement stmt = con.createStatement();
            String[] fileDetail = FileRead.readFile(inputFilePath, new String[0]);
            loadingResult = this.loadGenBankFile(fileDetail);
            writeGenBankFileIntoDB(loadingResult, stmt);
            con.close();
        }catch( Exception e ) {
            e.printStackTrace();
        }            
        return loadingResult;
    }
    
    public boolean writeToFile(NCBIGenBank loadingResult, Statement stmt) {
        boolean sucTag = true;
        try {
            ArrayList<String> mainSql = new ArrayList();
            //System.out.println(replacePrime(loadingResult.getDefinition()));
            //stmt.executeUpdate("UPDATE nucleotide SET definition='" + replacePrime(loadingResult.getDefinition()) + "' WHERE locus='" + loadingResult.getLocus() + "';\n");
            String writeString = loadingResult.getLocus() + "\t" + replacePrime(loadingResult.getFeaturesBasicInfo().getPCR_primers())+ "\t";
            String refString = "";
            for(int j=0; j<loadingResult.getReferenceList().size(); j++) {
                NCBIReference tempRef = loadingResult.getReferenceList().get(j);
                if(tempRef.getPubmed()!=0){
                    refString +=tempRef.getPubmed()+"$";
                }

            }
            if(!loadingResult.getFeaturesBasicInfo().getPCR_primers().equals("")) {
                FileWrite.writeInput(writeString+refString, "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\primer.txt");
            } else {
                System.out.println(loadingResult.getLocus());
            }
            /*
            mainSql.add("INSERT INTO nucleotide (locus, length, moleculetype, topology, division, modifydate, definition, " +
                    "accession, version, gi, dblink, keywords, source, organism, organtaxonomy, strain, isolate, origin, disease, country) VALUES('"+
                    loadingResult.getLocus()+"',"+loadingResult.getLength()+",'"+replacePrime(loadingResult.getMoleculeType())+"','"+replacePrime(loadingResult.getTopology())+"','"+
                    replacePrime(loadingResult.getDivision())+"','"+replacePrime(loadingResult.getModifydate())+"','"+replacePrime(loadingResult.getDefinition())+"','"+
                    replacePrime(loadingResult.getAccession())+"','"+replacePrime(loadingResult.getVersion())+"',"+replacePrime(loadingResult.getGi())+",'"+replacePrime(loadingResult.getFeaturesBasicInfo().getDb_xref())+"','"+
                    replacePrime(loadingResult.getKeywords())+"','"+replacePrime(loadingResult.getSource())+"','"+replacePrime(loadingResult.getOrganism())+"','"+replacePrime(loadingResult.getOrganTaxonomy())+"','"+
                    replacePrime(loadingResult.getFeaturesBasicInfo().getStrain())+"','"+replacePrime(loadingResult.getFeaturesBasicInfo().getIsolate())+"','"+
                    loadingResult.getOrgin()+"','','"+replacePrime(loadingResult.getFeaturesBasicInfo().getCountry())+"');\n");
            for(int j=0; j<loadingResult.getReferenceList().size(); j++) {
                NCBIReference tempRef = loadingResult.getReferenceList().get(j);
                ResultSet tempResult = stmt.executeQuery("SELECT ref_index FROM reference where pubmed = "+tempRef.getPubmed()+" and title='"+replacePrime(tempRef.getTitle())+"' and journal='"+replacePrime(tempRef.getJournal())+"'");
                if (tempResult.next()) {
                    mainSql.add("INSERT INTO nucleotide_reference (locus, ref_index, ref_num, ref_des) VALUES('"+loadingResult.getLocus()+"', "+tempResult.getString(1)+","+tempRef.getNumber()+",'"+tempRef.getDetail()+"');\n");
                } else {
                    mainSql.add("INSERT INTO reference (pubmed, authors, title, journal) VALUES("+tempRef.getPubmed()+",'"+replacePrime(tempRef.getAuthors())+"','"+replacePrime(tempRef.getTitle())+"','"+replacePrime(tempRef.getJournal())+"');\n");
                    mainSql.add("INSERT INTO nucleotide_reference (locus, ref_index, ref_num, ref_des) VALUES('"+loadingResult.getLocus()+"', LAST_INSERT_ID(),"+tempRef.getNumber()+",'"+tempRef.getDetail()+"');\n");
                }
            }
            for(int j=0; j<loadingResult.getFeaturesCDSInfo().size(); j++) {
                NCBIGenBankFeaturesCDS tempCDS = loadingResult.getFeaturesCDSInfo().get(j);
                mainSql.add("INSERT INTO cds (locus, cds_index, gene_region, gene_gene, gene_locus_tag, gene_db_xref, cds_region, cds_gene, cds_locus_tag, cds_product, cds_protein_id, cds_EC_number, cds_note, cds_db_xref, cds_translation, " +
                        "cds_codon_start, cds_function) VALUES('"+loadingResult.getLocus()+"',"+tempCDS.getIndex()+",'"+replacePrime(tempCDS.getGene_region())+"','"+replacePrime(tempCDS.getGene_gene())+"','"+replacePrime(tempCDS.getGene_locus_tag())+"','"+
                        replacePrime(tempCDS.getGene_db_xref())+"','"+replacePrime(tempCDS.getCDS_region())+"','"+replacePrime(tempCDS.getCDS_gene())+"','"+replacePrime(tempCDS.getCDS_locus_tag())+"','"+replacePrime(tempCDS.getCDS_product())+"','"+
                        replacePrime(tempCDS.getCDS_protein_id())+"','"+replacePrime(tempCDS.getCDS_EC_number())+"','"+replacePrime(tempCDS.getCDS_note())+"','"+replacePrime(tempCDS.getCDS_db_xref())+"','"+replacePrime(tempCDS.getCDS_translation())+"',"+
                        tempCDS.getCDS_codon_start()+",'"+replacePrime(tempCDS.getCDS_function())+"');\n");
            }
            for(int j=0;j<mainSql.size();j++) {
                stmt.executeUpdate(mainSql.get(j).toString());
            }*/
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            sucTag = false;
            e.printStackTrace();
        }
        return sucTag;
    }

    public boolean writeGenBankFileIntoDB(NCBIGenBank loadingResult, Statement stmt) {
        boolean sucTag = true;
        try {
            ArrayList<String> mainSql = new ArrayList();
            mainSql.add("INSERT INTO nucleotide (locus, length, moleculetype, topology, division, modifydate, definition, " +
                    "accession, version, gi, dblink, keywords, source, organism, organtaxonomy, strain, isolate, origin, disease, country) VALUES('"+
                    loadingResult.getLocus()+"',"+loadingResult.getLength()+",'"+replacePrime(loadingResult.getMoleculeType())+"','"+replacePrime(loadingResult.getTopology())+"','"+
                    replacePrime(loadingResult.getDivision())+"','"+replacePrime(loadingResult.getModifydate())+"','"+replacePrime(loadingResult.getDefinition())+"','"+
                    replacePrime(loadingResult.getAccession())+"','"+replacePrime(loadingResult.getVersion())+"',"+replacePrime(loadingResult.getGi())+",'"+replacePrime(loadingResult.getFeaturesBasicInfo().getDb_xref())+"','"+
                    replacePrime(loadingResult.getKeywords())+"','"+replacePrime(loadingResult.getSource())+"','"+replacePrime(loadingResult.getOrganism())+"','"+replacePrime(loadingResult.getOrganTaxonomy())+"','"+
                    replacePrime(loadingResult.getFeaturesBasicInfo().getStrain())+"','"+replacePrime(loadingResult.getFeaturesBasicInfo().getIsolate())+"','"+
                    loadingResult.getOrgin()+"','','"+replacePrime(loadingResult.getFeaturesBasicInfo().getCountry())+"');\n");
            for(int j=0; j<loadingResult.getReferenceList().size(); j++) {
                NCBIReference tempRef = loadingResult.getReferenceList().get(j);
                ResultSet tempResult = stmt.executeQuery("SELECT ref_index FROM reference where pubmed = "+tempRef.getPubmed()+" and title='"+replacePrime(tempRef.getTitle())+"' and journal='"+replacePrime(tempRef.getJournal())+"'");
                if (tempResult.next()) {
                    mainSql.add("INSERT INTO nucleotide_reference (locus, ref_index, ref_num, ref_des) VALUES('"+loadingResult.getLocus()+"', "+tempResult.getString(1)+","+tempRef.getNumber()+",'"+tempRef.getDetail()+"');\n");
                } else {
                    mainSql.add("INSERT INTO reference (pubmed, authors, title, journal) VALUES("+tempRef.getPubmed()+",'"+replacePrime(tempRef.getAuthors())+"','"+replacePrime(tempRef.getTitle())+"','"+replacePrime(tempRef.getJournal())+"');\n");
                    mainSql.add("INSERT INTO nucleotide_reference (locus, ref_index, ref_num, ref_des) VALUES('"+loadingResult.getLocus()+"', LAST_INSERT_ID(),"+tempRef.getNumber()+",'"+tempRef.getDetail()+"');\n");
                }
            }
            for(int j=0; j<loadingResult.getFeaturesCDSInfo().size(); j++) {
                NCBIGenBankFeaturesCDS tempCDS = loadingResult.getFeaturesCDSInfo().get(j);
                mainSql.add("INSERT INTO cds (locus, cds_index, gene_region, gene_gene, gene_locus_tag, gene_db_xref, cds_region, cds_gene, cds_locus_tag, cds_product, cds_protein_id, cds_EC_number, cds_note, cds_db_xref, cds_translation, " +
                        "cds_codon_start, cds_function) VALUES('"+loadingResult.getLocus()+"',"+tempCDS.getIndex()+",'"+replacePrime(tempCDS.getGene_region())+"','"+replacePrime(tempCDS.getGene_gene())+"','"+replacePrime(tempCDS.getGene_locus_tag())+"','"+
                        replacePrime(tempCDS.getGene_db_xref())+"','"+replacePrime(tempCDS.getCDS_region())+"','"+replacePrime(tempCDS.getCDS_gene())+"','"+replacePrime(tempCDS.getCDS_locus_tag())+"','"+replacePrime(tempCDS.getCDS_product())+"','"+
                        replacePrime(tempCDS.getCDS_protein_id())+"','"+replacePrime(tempCDS.getCDS_EC_number())+"','"+replacePrime(tempCDS.getCDS_note())+"','"+replacePrime(tempCDS.getCDS_db_xref())+"','"+replacePrime(tempCDS.getCDS_translation())+"',"+
                        tempCDS.getCDS_codon_start()+",'"+replacePrime(tempCDS.getCDS_function())+"');\n");
            }
            for(int j=0;j<mainSql.size();j++) {
                stmt.executeUpdate(mainSql.get(j).toString());
            }
        } catch (Exception e) {
            System.out.println(loadingResult.getLocus()+" has some problem");
            sucTag = false;
            e.printStackTrace();
        }
        return sucTag;
    }

    public NCBIGenBank loadGenBankFile(String[] fileDetail) {
        NCBIGenBank loadingResult = new NCBIGenBank();
        int i = 0;
        boolean printTag = false;
        try{
            int cds_index = 0;
            ArrayList<NCBIReference> referenceList = new ArrayList<NCBIReference>();
            NCBIGenBankFeaturesBasic featuresBasicInfo;
            NCBIGenBankFeaturesRepeat featuresRepeatInfo = new NCBIGenBankFeaturesRepeat();
            ArrayList<NCBIGenBankFeaturesCDS> featuresCDSInfo = new ArrayList<NCBIGenBankFeaturesCDS>();
            while(i<fileDetail.length) {
                String tag = fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim();
                if(tag.equals("LOCUS")) {
                    loadingResult.setLocus(fileDetail[i].substring(12, 28).trim());
                    loadingResult.setLength(Integer.parseInt(fileDetail[i].substring(29, 40).trim()));
                    loadingResult.setMoleculeType(fileDetail[i].substring(47, 53).trim());
                    loadingResult.setTopology(fileDetail[i].substring(55, 63).trim());
                    loadingResult.setDivision(fileDetail[i].substring(64, 67).trim());
                    String modifyDate=fileDetail[i].substring(68, 79).trim();
                    SimpleDateFormat oldFormat = new SimpleDateFormat("dd-MMM-yyyy");
                    SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
                    modifyDate=newFormat.format(oldFormat.parse(modifyDate));
                    loadingResult.setModifydate(modifyDate);
                } else if(tag.equals("DEFINITION")) {
                    String definition = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        definition += " "+fileDetail[i].substring(12, fileDetail[i].length()).trim();
                        i++;
                    }
                    i--;
                    loadingResult.setDefinition(definition);
                } else if(tag.equals("ACCESSION")) {
                    loadingResult.setAccession(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                } else if(tag.equals("VERSION")) {
                    loadingResult.setVersion(fileDetail[i].substring(12, fileDetail[i].indexOf("GI:")).trim());
                    loadingResult.setGi(fileDetail[i].substring(fileDetail[i].indexOf("GI:")+3, fileDetail[i].length()).trim());
                } else if(tag.equals("KEYWORDS")) {
                    loadingResult.setKeywords(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                } else if(tag.equals("SOURCE")) {
                    loadingResult.setSource(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                } else if(tag.equals("ORGANISM")) {
                    loadingResult.setOrganism(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                    String organText = "";
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        organText+=fileDetail[i].trim();
                        i++;
                    }
                    i--;
                    loadingResult.setOrganTaxonomy(organText);
                } else if(tag.equals("REFERENCE")) {
                    int refNum = 0;
                    String refDetail = "";
                    String authors = "";
                    String title = "";
                    String journal = "";
                    int pubmed = 0;
                    if(fileDetail[i].indexOf("(")!=-1){
                        refNum = Integer.parseInt(fileDetail[i].substring(12, fileDetail[i].indexOf("(")).trim());
                        refDetail = fileDetail[i].substring(fileDetail[i].indexOf("("), fileDetail[i].length()).trim();
                    } else {
                        refNum = Integer.parseInt(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                        refDetail = "";
                    }
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=2?2:fileDetail[i].length())).trim().equals("")){
                        String tempTag = fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim();
                        if(tempTag.equals("AUTHORS")) {
                            authors += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                authors += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("TITLE")) {
                            title += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                title += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("JOURNAL")) {
                            journal += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                journal += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("PUBMED")) {
                            pubmed = Integer.parseInt(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                        } else {
                            if(printTag==true)
                                System.out.println(i+"\t"+fileDetail[i]);
                        }
                        i++;
                    }
                    i--;
                    referenceList.add(new NCBIReference(refNum, refDetail, authors, title, journal, pubmed, ""));
                /**/
                } else if(tag.equals("FEATURES")) {
                    String source = "";
                    String organism = "";
                    String db_xref = "";
                    String host = "";
                    String map = "";
                    String strain = "";
                    String mol_type = "";
                    String isolate = "";
                    String note = "";
                    String country = "";
                    String PCR_primers = "";
                    String CDS_region = "";
                    String CDS_inference = "";
                    int CDS_codon_start = 0;
                    int CDS_transl_table = 0;
                    String CDS_product = "";
                    String CDS_protein_id = "";
                    String CDS_translation = "";
                    String CDS_db_xref = "";
                    String CDS_note = "";
                    String CDS_gene = "";                    
                    String CDS_locus_tag = "";
                    String CDS_EC_number = "";
                    String CDS_function = "";
                    String gene_region = "";
                    String gene_gene = "";
                    String gene_locus_tag = "";
                    String gene_db_xref = "";
                    i++;
                    int geneTag = 0;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=2?2:fileDetail[i].length())).trim().equals("")){
                        String tempTag = fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim();
                        if(tempTag.equals("source")) {
                            source = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/organism")!=-1) {
                                    organism = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim() + "@";
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/host")!=-1) {
                                    host = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        host += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(host.lastIndexOf("\"")!=-1){
                                        host = host.substring(0, host.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/map")!=-1) {
                                    map = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        map += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(map.lastIndexOf("\"")!=-1){
                                        map = map.substring(0, map.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/strain")!=-1) {
                                    strain = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/mol_type")!=-1) {
                                    mol_type = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/isolate")!=-1) {
                                    isolate = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/note")!=-1) {
                                    note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(note.lastIndexOf("\"")!=-1){
                                        note = note.substring(0, note.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/country")!=-1) {
                                    country = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        country += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(country.lastIndexOf("\"")!=-1){
                                        country = country.substring(0, country.lastIndexOf("\""));
                                    }
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/PCR_primers")!=-1) {
                                    PCR_primers = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        PCR_primers += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(PCR_primers.lastIndexOf("\"")!=-1){
                                        PCR_primers = PCR_primers.substring(0, PCR_primers.lastIndexOf("\""));
                                    }
                                    //System.out.println(gene_db_xref);
                                } else {
                                    if(printTag==true)
                                        System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                            //System.out.println(features_source);
                        /**/
                        } else if(tempTag.equals("gene")) {
                            geneTag=1;
                            gene_region = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/gene")!=-1) {
                                    gene_gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/locus_tag")!=-1) {
                                    gene_locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    gene_db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.length()-1).trim() + "@";
                                    //System.out.println(gene_db_xref);
                                } else {
                                    if(printTag==true)
                                        System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("CDS")) {
                            cds_index++;
                            if(geneTag==0){
                                gene_region = "";
                                gene_gene = "";
                                gene_locus_tag = "";
                                gene_db_xref = "";
                            }
                            CDS_region = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            //System.out.println(CDS_region);
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/inference")!=-1){
                                    CDS_inference = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_inference += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_inference.lastIndexOf("\"")!=-1){
                                        CDS_inference = CDS_inference.substring(0, CDS_inference.lastIndexOf("\""));
                                    }
                                    //System.out.println(codon_start);
                                } else if(tempString.indexOf("/codon_start")!=-1){
                                    CDS_codon_start = Integer.parseInt(tempString.substring(tempString.indexOf("=")+1, tempString.length()).trim());
                                    //System.out.println(codon_start);
                                } else if(tempString.indexOf("/transl_table")!=-1){
                                    CDS_transl_table = Integer.parseInt(tempString.substring(tempString.indexOf("=")+1, tempString.length()).trim());
                                    //System.out.println(codon_start);
                                } else if(tempString.indexOf("/gene=")!=-1) {
                                    CDS_gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/locus_tag")!=-1) {
                                    CDS_locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/note")!=-1) {
                                    CDS_note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_note.lastIndexOf("\"")!=-1){
                                        CDS_note = CDS_note.substring(0, CDS_note.lastIndexOf("\""));
                                    }
                                    //System.out.println(note);
                                } else if(tempString.indexOf("/product")!=-1) {
                                    CDS_product = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_product += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_product.lastIndexOf("\"")!=-1){
                                        CDS_product = CDS_product.substring(0, CDS_product.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/protein_id")!=-1) {
                                    CDS_protein_id = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(protein_id);
                                } else if(tempString.indexOf("/EC_number")!=-1) {
                                    CDS_EC_number = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(protein_id);
                                } else if(tempString.indexOf("/translation")!=-1) {
                                    CDS_translation = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_translation += fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_translation.lastIndexOf("\"")!=-1){
                                        CDS_translation = CDS_translation.substring(0, CDS_translation.lastIndexOf("\""));
                                    }
                                    //System.out.println(translation);
                                } else if(tempString.indexOf("/function")!=-1) {
                                    CDS_function = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_function += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_function.lastIndexOf("\"")!=-1){
                                        CDS_function = CDS_function.substring(0, CDS_function.lastIndexOf("\""));
                                    }
                                    //System.out.println(function);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    CDS_db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.length()-1).trim() + "@";
                                    //System.out.println(db_xref);
                                } else {
                                    if(printTag==true)
                                        System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                            featuresCDSInfo.add(new NCBIGenBankFeaturesCDS(cds_index, CDS_region, CDS_inference, CDS_codon_start, CDS_transl_table, CDS_product,
                                    CDS_protein_id, CDS_db_xref, CDS_translation, CDS_note, CDS_gene, CDS_locus_tag, CDS_EC_number, CDS_function,
                                    gene_region, gene_gene, gene_locus_tag, gene_db_xref));
                            //clear
                            CDS_region = "";
                            CDS_inference = "";
                            CDS_codon_start = 0;
                            CDS_transl_table = 0;
                            CDS_product = "";
                            CDS_protein_id = "";
                            CDS_translation = "";
                            CDS_db_xref = "";
                            CDS_note = "";
                            CDS_gene = "";
                            CDS_locus_tag = "";
                            CDS_EC_number = "";
                            CDS_function = "";
                            gene_region = "";
                            gene_gene = "";
                            gene_locus_tag = "";
                            gene_db_xref = "";
                            geneTag = 0;
                         /**
                         */
                        } else {
                            if(printTag==true)
                                System.out.println(i+"\t"+fileDetail[i]);
                        }
                        i++;
                    }
                    i--;
                    featuresBasicInfo = new NCBIGenBankFeaturesBasic(source, organism, db_xref, host, map, strain, mol_type, isolate, note, country, PCR_primers);
                    loadingResult.setFeaturesBasicInfo(featuresBasicInfo);
                    loadingResult.setFeaturesCDSInfo(featuresCDSInfo);
                    //loadingResult.setFeaturesRepeatInfo(featuresRepeatInfo);
                //
//                } else if(tag.equals("ORIGIN")) {
//                    String sequence = fileDetail[i].substring(10, fileDetail[i].length()).trim();
//                    i++;
//                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=2?2:fileDetail[i].length())).trim().equals("")){
//                        sequence+= " "+fileDetail[i].substring(10, fileDetail[i].length()).trim();
//                        i++;
//                    }
//                    i--;
//                    loadingResult.setOrgin(sequence.trim());
                } else if(tag.equals("//")) {
                    break;
                } else {
                    if(printTag==true)
                        System.out.println(i+"\t"+fileDetail[i]);
                }
                i++;
            }
            loadingResult.setReferenceList(referenceList);
        }catch( Exception e ) {
            System.out.println(loadingResult.getLocus()+"\t"+i+"\t"+fileDetail[i]);
            e.printStackTrace();
        }

        return loadingResult;
    }

    public String replacePrime(String inputString) {
        return inputString.replace("'", "\\'");
    }
}
